﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace CommonFunction
{
    public class PCHelper : PCInterface
    {
        #region 获取主板序列号
        public string GetMBSN()
        {
            //获取主板序列号
            string mbsn = "";
            ManagementClass mc = new ManagementClass("win32_baseboard");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                mbsn = mo.Properties["SerialNumber"].Value.ToString();
                break;
            }
            return mbsn;
        }
        #endregion
        #region 获取CPU型号
        public string GetCPUModel()
        {
            string cpuModel = "";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject obj in searcher.Get())
            {
                cpuModel = obj["Name"].ToString();
                break;
            }
            return cpuModel;
        }
        #endregion
        #region 获取CPU占用

        public float GetCpuUsage()
        {
            PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            float totalCpuUsage = 0f;

            for (int i = 0; i < 5; i++)
            {
                // 获取两个采样之间的 CPU 时间占用率
                float cpuUsage1 = cpuCounter.NextValue();
                Thread.Sleep(200);
                float cpuUsage2 = cpuCounter.NextValue();
                float deltaCpuUsage = cpuUsage2 - cpuUsage1;

                // 将 CPU 时间占用率累加到总值
                totalCpuUsage += deltaCpuUsage;
            }

            // 计算前1秒的 CPU 时间占用率平均值
            float avgCpuUsage = totalCpuUsage / 5f;

            return avgCpuUsage;
        }
        #endregion
        #region 获取GPU型号
        public string GetGPUModel()
        {
            string gpuModel = "";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_VideoController");
            foreach (ManagementObject obj in searcher.Get())
            {
                gpuModel = obj["Name"].ToString();
                break;
            }
            return gpuModel;
        }

        #endregion
        #region 获取内存大小
        public double GetTotalMemorySize()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
            foreach (ManagementObject mo in searcher.Get())
            {
                double freeMemory = double.Parse(mo["FreePhysicalMemory"].ToString());
                double totalMemory = double.Parse(mo["TotalVisibleMemorySize"].ToString());
                return (float)(totalMemory / (1024 * 1024));
            }

            return 0;
        }
        #endregion
        #region 获取已经使用的内存
        public double GetCurrentMemoryUsage()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
            foreach (ManagementObject mo in searcher.Get())
            {
                double freeMemory = double.Parse(mo["FreePhysicalMemory"].ToString());
                double totalMemory = double.Parse(mo["TotalVisibleMemorySize"].ToString());
                return (float)((totalMemory - freeMemory) / (1024 * 1024));
            }

            return 0;
        }
        #endregion
        #region 获取有哪些盘
        public string[] GetDriveNames()
        {
            // 获取所有驱动器的名称
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            // 存储驱动器名称的字符串数组
            string[] driveNames = new string[allDrives.Length];

            // 遍历所有驱动器并将其名称存储到数组中
            for (int i = 0; i < allDrives.Length; i++)
            {
                driveNames[i] = allDrives[i].Name;
            }

            // 返回所有驱动器名称的数组
            return driveNames;
        }
        #endregion

        #region 获取桌面地址
        public string GetDeskPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        }
        #endregion

        #region 隐藏桌面
        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);

        private const int SW_HIDE = 0;
        private const int SW_SHOW = 1;

        public void HideTaskbar()
        {
            int hwnd = FindWindow("Shell_TrayWnd", "");
            ShowWindow(hwnd, SW_HIDE);
        }

        public void ShowTaskbar()
        {
            int hwnd = FindWindow("Shell_TrayWnd", "");
            ShowWindow(hwnd, SW_SHOW);
        }
        #endregion


    }

    interface PCInterface
    {
        ///<summary>
        /// 获取主板序列号
        ///</summary>
        string GetMBSN();
        /// <summary>
        /// 获取CPU型号
        /// </summary>
        /// <returns></returns>
        string GetCPUModel();
        /// <summary>
        /// 获取CPU当前占用情况
        /// </summary>
        /// <returns></returns>
        float GetCpuUsage();
        /// <summary>
        /// 获取GPU型号
        /// </summary>
        /// <returns></returns>
        string GetGPUModel();
        /// <summary>
        /// 获取电脑内存大小，返回值单位为GB
        /// </summary>
        /// <returns></returns>
        double GetTotalMemorySize();
        /// <summary>
        /// 获取电脑已经使用的内存，返回值单位为GB
        /// </summary>
        /// <returns></returns>
        double GetCurrentMemoryUsage();
        /// <summary>
        /// 获取所有盘符名称
        /// </summary>
        /// <returns></returns>
        string[] GetDriveNames();
        /// <summary>
        /// 获取桌面地址
        /// </summary>
        /// <returns></returns>
        string GetDeskPath();
        /// <summary>
        /// 隐藏任务栏
        /// </summary>
        void HideTaskbar();
        /// <summary>
        /// 显示任务栏
        /// </summary>
        void ShowTaskbar();
    }
}
